package com.softWang.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.softWang.common.aop.annotation.SysOperationLog;
import com.softWang.common.response.PageBean;
import com.softWang.common.request.Idparam;
import com.softWang.common.response.ResponseResult;
import com.softWang.system.constants.SysModel;
import com.softWang.system.controller.vo.systemDictType.SystemDictTypeQueryAllParam;
import com.softWang.system.controller.vo.systemDictType.SystemDictTypeQueryAllResult;
import com.softWang.system.po.SystemDictType;
import com.softWang.system.service.SystemDictTypeService;
import com.softWang.system.controller.vo.systemDictType.SystemDictTypeAddParam;
import com.softWang.system.controller.vo.systemDictType.SystemDictTypeDetailResult;
import com.softWang.system.controller.vo.systemDictType.SystemDictTypeEditParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@Api(value="字典类型管理",tags="字典类型管理")
@RequestMapping("/system/dictType")
public class SystemDictTypeController {
    @Resource
    private SystemDictTypeService dictTypeService;

    @PreAuthorize("@ss.hasPermi('system:dictType:query')")
    @ApiOperation(value="字典类型列表(分页)")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public ResponseResult<PageBean<SystemDictTypeQueryAllResult>> queryall(@RequestBody @Validated SystemDictTypeQueryAllParam param, HttpServletRequest request) {
        List<SystemDictTypeQueryAllResult> results=dictTypeService.queryall(param);
        PageBean page=new PageBean(results);
        return ResponseResult.success(page);
    }

    @PreAuthorize("@ss.hasPermi('system:dictType:add')")
    @ApiOperation(value="添加字典类型")
    @SysOperationLog(value = "添加字典类型", model = SysModel.DICTIONARY_MANAGE)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    //@Repeat(message = "重复提交")
    public ResponseResult<Long> add(@RequestBody @Validated SystemDictTypeAddParam param, HttpServletRequest request) throws Exception {
        SystemDictType dictType = new SystemDictType();
        BeanUtils.copyProperties(param, dictType);
        Long id = dictTypeService.add(dictType);
        return ResponseResult.success(id);
    }

    @PreAuthorize("@ss.hasPermi('system:dictType:edit')")
    @ApiOperation(value="修改字典类型")
    @ApiOperationSupport(ignoreParameters = {"state","ctime","utime","cuser","euser","deleted"})
    @SysOperationLog(value = "修改字典类型", model = SysModel.DICTIONARY_MANAGE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResponseResult<Long> edit(@RequestBody @Validated SystemDictTypeEditParam param, HttpServletRequest request) {
        SystemDictType dictType = new SystemDictType();
        BeanUtils.copyProperties(param, dictType);
        Long id = dictTypeService.edit(dictType);
        return ResponseResult.success(id);
    }


    @PreAuthorize("@ss.hasPermi('system:dictType:detail')")
    @ApiOperation(value="查看详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ResponseResult<SystemDictTypeDetailResult> detail(@RequestBody @Validated Idparam idparam) {        ;
        SystemDictTypeDetailResult result = new SystemDictTypeDetailResult();
        SystemDictType dictType = dictTypeService.getById(idparam.getId());
        BeanUtils.copyProperties(dictType, result);
        return ResponseResult.success(result);
    }


    @PreAuthorize("@ss.hasPermi('system:dictType:del')")
    @ApiOperation(value="删除字典类型")
    @SysOperationLog(value = "删除字典类型", model = SysModel.DICTIONARY_MANAGE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResponseResult del(@RequestBody @NotEmpty(message = "ids 不能为空") List<@NotNull(message = "ids 不能为空") Long> ids, HttpServletRequest request) {
        dictTypeService.del(ids);
        return ResponseResult.success();
    }

}
